//
//  ZMyAddressCell.m
//  DistributorAB
//
//  Created by LZY on 2019/10/11.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZMyAddressCell.h"
@interface ZMyAddressCell ()
@property (nonatomic, strong) UILabel *lblName;//名字
@property (nonatomic, strong) UILabel *lblPhone;//手机号
@property (nonatomic, strong) UILabel *lblAddress;//地址
@property (nonatomic, strong) UIButton *btnCycle;//选中按钮
@property (nonatomic, strong) UIImageView *ivCycle;//选中图片
@end

@implementation ZMyAddressCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupViewAction];
    }
    return self;
}
#pragma mark - 界面布局
- (void)setupViewAction{
    self.contentView.backgroundColor = kColorWithHex(0xf2f2f2);
    
    UIView *viewBg = [UIView new];
    viewBg.backgroundColor = kColorWithHex(0xffffff);
    viewBg.layer.cornerRadius = 4;
    viewBg.layer.masksToBounds = YES;
    [self.contentView addSubview:viewBg];
    [viewBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(1);
        make.leading.trailing.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-1);
    }];
    
    _lblName = [UILabel new];
    _lblName.font = [UIFont systemFontOfSize:14];
    _lblName.textColor = kColorWithHex(0x666666);
    _lblName.textAlignment = NSTextAlignmentLeft;
    _lblName.preferredMaxLayoutWidth = 200;
    [viewBg addSubview:_lblName];
    [_lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBg.mas_top).offset(16);
        make.leading.equalTo(viewBg.mas_leading).offset(16);
        make.height.equalTo(@(20));
        make.width.equalTo(@(200));
    }];
    
    _lblPhone = [UILabel new];
    _lblPhone.font = [UIFont systemFontOfSize:14];
    _lblPhone.textColor = kColorWithHex(0x666666);
    _lblPhone.textAlignment = NSTextAlignmentRight;
    [viewBg addSubview:_lblPhone];
    [_lblPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBg.mas_top).offset(16);
        make.trailing.equalTo(viewBg.mas_trailing).offset(-15);
        make.height.equalTo(@(20));
    }];
    
    _lblAddress = [UILabel new];
    _lblAddress.font = [UIFont systemFontOfSize:12];
    _lblAddress.textColor = kColorWithHex(0x666666);
    _lblAddress.textAlignment = NSTextAlignmentLeft;
    _lblAddress.preferredMaxLayoutWidth = kScreen_Width - 31;
    _lblAddress.numberOfLines = 0;
    [viewBg addSubview:_lblAddress];
    [_lblAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lblName.mas_bottom).offset(12);
        make.leading.equalTo(viewBg.mas_leading).offset(16);
        make.trailing.equalTo(viewBg.mas_trailing).offset(-15);
    }];
    
    UIView *viewLine = [UIView new];
    viewLine.backgroundColor = kColorWithHex(0xf2f2f2);
    [viewBg addSubview:viewLine];
    [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(viewBg);
        make.top.equalTo(self.lblAddress.mas_bottom).offset(3);
    }];
    
    _ivCycle = [UIImageView new];
    _ivCycle.image = imgname(@"cycle_normal_gray_icon");//cycle_selected_green_icon
    [viewBg addSubview:_ivCycle];
    [_ivCycle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(viewBg.mas_leading).offset(16);
        make.top.equalTo(viewLine.mas_bottom).offset(19);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    UILabel *lblNotice = [UILabel new];
    lblNotice.text = @"设为默认地址";
    lblNotice.font = [UIFont systemFontOfSize:12];
    lblNotice.textColor = kColorWithHex(0x707070);
    [viewBg addSubview:lblNotice];
    [lblNotice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.ivCycle.mas_centerY);
        make.leading.equalTo(self.ivCycle.mas_trailing).offset(3);
    }];
    
    _btnCycle = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnCycle addTarget:self action:@selector(btnCycleClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBg addSubview:_btnCycle];
    [_btnCycle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.ivCycle.mas_centerY);
        make.leading.equalTo(self.ivCycle.mas_leading).offset(-6);
        make.trailing.equalTo(lblNotice.mas_trailing);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnDelete setTitle:@"删除" forState:UIControlStateNormal];
    [btnDelete setTitleColor:kColorWithHex(0x999999) forState:UIControlStateNormal];
    btnDelete.titleLabel.font = [UIFont systemFontOfSize:12];
    btnDelete.layer.cornerRadius = 4;
    btnDelete.layer.masksToBounds = YES;
    btnDelete.layer.borderColor = kColorWithHex(0x999999).CGColor;
    btnDelete.layer.borderWidth = 1;
    [btnDelete addTarget:self action:@selector(btnDeleteClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBg addSubview:btnDelete];
    [btnDelete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewLine.mas_bottom).offset(15);
        make.trailing.equalTo(viewBg.mas_trailing).offset(-15);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-15);
        make.size.mas_equalTo(CGSizeMake(48, 24));
    }];
    
    UIButton *btnEdit = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnEdit setTitle:@"编辑" forState:UIControlStateNormal];
    [btnEdit setTitleColor:kColorWithHex(0x999999) forState:UIControlStateNormal];
    btnEdit.titleLabel.font = [UIFont systemFontOfSize:12];
    btnEdit.layer.cornerRadius = 4;
    btnEdit.layer.masksToBounds = YES;
    btnEdit.layer.borderColor = kColorWithHex(0x999999).CGColor;
    btnEdit.layer.borderWidth = 1;
    [btnEdit addTarget:self action:@selector(btnEditClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBg addSubview:btnEdit];
    [btnEdit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnDelete.mas_top);
        make.trailing.equalTo(btnDelete.mas_leading).offset(-24);
        make.size.mas_equalTo(CGSizeMake(48, 24));
    }];
    
}
#pragma mark - 数据更新
- (void)setModel:(ZMyAddressModel *)model{
    if (model) {
        _model = model;
        _lblName.text = model.user_name;
        _lblPhone.text = model.phone;

           _lblAddress.text = model.address;
       //  stringByReplacingOccurrencesOfString:@" " withString:@""];
        if ([model.is_default isEqualToString:@"0"]) {
            //未选中
            _ivCycle.image = imgname(@"cycle_normal_gray_icon");
        }else{
            //选中
            _ivCycle.image = imgname(@"cycle_selected_green_icon");
        }
    }
}
#pragma mark - 交互事件
- (void)btnCycleClickAction:(UIButton *)sender{
    if ([_model.is_default isEqualToString:@"0"]) {
        //设为默认地址
        kWeakSelf;
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObjectIfNotNil:_model.Id forKey:@"id"];
        [NetWorkTool POST:setAgentDefaultAddress param:dict success:^(id dic) {
            ShowToastWithText(@"默认地址设置成功");
            weakSelf.ivCycle.image = imgname(@"cycle_normal_gray_icon");
            [[NSNotificationCenter defaultCenter] postNotificationName:USER_ADD_ADDRESS_SUCCESS object:nil];
            
            #pragma mark - 默认地址保存
            NSData *addressData = [NSKeyedArchiver archivedDataWithRootObject:self->_model];
            [USER_DEFAULT setObject:addressData forKey:DEFAULT_ADDRESS];
            [USER_DEFAULT synchronize];
            if (self.delegate && [self.delegate respondsToSelector:@selector(addressDefaultClickActionindexPath:)]) {
                [self.delegate addressDefaultClickActionindexPath:self->_indexPath];
            }

            
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
        } fail:^(NSError *error) {
            
        } needUser:YES];
        
    }
}
- (void)btnDeleteClickAction:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addressCellClickAction:indexPath:)]) {
        [self.delegate addressCellClickAction:ZMyAddressCellActionTypeDelete indexPath:_indexPath];
    }
}
- (void)btnEditClickAction:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addressCellClickAction:indexPath:)]) {
        [self.delegate addressCellClickAction:ZMyAddressCellActionTypeEdit indexPath:_indexPath];
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
